package com.qianji.controller;

import com.qianji.pojo.PVUVDto;
import com.qianji.pojo.UserDto;
import com.qianji.service.UserService;
import com.qianji.utils.JwtUtils;
import com.qianji.utils.Result;
import com.qianji.vo.PVUVVO;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test/")
public class TestController {

    @Autowired
    private UserService userService;

    @Autowired
    private PVUVController pvuvController;

    @RequestMapping(value = "test",method = RequestMethod.POST)
    public void test(){
        for (int i = 0; i < 3000; i++) {
            PVUVVO pvuvvo = new PVUVVO();
            pvuvvo.setTimestamp("1624526325000");
            PVUVDto dto = new PVUVDto();
            Long l = 1623917757981L+i;
            dto.setImei(String.valueOf(l));
            dto.setActiveId(66L);
            dto.setLinkId(127L);
            dto.setLinkUrl("https://www.baidu.com");
            dto.setPanId(216L);
            pvuvvo.setData(dto);
            pvuvController.addUV(pvuvvo);
        }
    }

    @RequestMapping(value = "logic",method = RequestMethod.POST)
    public Result logicTest(String name,String password){

        String token = JwtUtils.createJwtToken(name,password);

        return new Result(token);

    }

    @RequestMapping(value = "check",method = RequestMethod.POST)
    public Result checkToken(String token){
        //String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6IjE1OTE0MjI2MzU1ODQiLCJ1c2VyTmFtZSI6ImFkbWluIiwiZXhwIjoxNjE5NTk0MDU0fQ.rOVJpU4Ntuq9LJFBMDFazPrYOqRR9f4Qg47hFmCnokc";

        if(token == null){
            return new Result(999,"跳转登录页面");
        }

        //获取token中的用户名和密码
        Map<String,Object> map = JwtUtils.checkJwtToken(token);

        if(map == null){
            return new Result(999,"校验不通过，跳转登录页面");
        }

        UserDto userDto = new UserDto();
        userDto.setUserName((String) map.get("userName"));
        userDto.setPassword((String) map.get("password"));

        List<UserDto> userDtoList =userService.findUser(userDto);
        if(userDtoList == null){
            return new Result(999,"该用户不存在，跳转登录页面");
        }
        //判断map是否为null
        //不为null则进行下一步，为null则跳转登录页面
        //校验用户名和密码是否存在
        //存在则直接登录，不存在则跳转到用户登录页面

        return new Result(200,"验证通过，跳转页面");

    }



}
